<template>


  <el-button type="primary" @click="add"> 撸管一次记录一次</el-button>
  <div style="margin: 10px 0;">
    <el-input v-model="search" placeholder="输入" style="width: 20%" clearable></el-input>
    <el-button type="primary" @click="load" > 查询光辉事迹</el-button>
  </div>

  <div>
    <el-table
        :data="tableData"
        stripe
        style="width: 100%">
      <el-table-column
          prop="id"
          label="ID"
          sortable
          width="220">
      </el-table-column>
      <el-table-column
          prop="time"
          label="日期"
          sortable=""
          width="220">
      </el-table-column>
      <el-table-column
          prop="name"
          label="姓名"
          width="220">
      </el-table-column>
      <el-table-column
          prop="cishu"
          label="次数"
          width="220">
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)"
          >编辑</el-button
          >
          <el-popconfirm title="确认删除吗？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
            <el-button
                size="small"
                type="danger"
            >删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
        title="记录你的喷射"
        v-model="dialogVisible"
        width="30%">
      <el-form :model="form" label-width="50px">
        <el-form-item label="名字">
          <el-input v-model="form.name" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="次数">
          <el-input-number v-model="form.cishu" style="width: 80%"></el-input-number>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
              v-model="form.time"
              type="datetime"
              placeholder="选择喷射时间"
              format="YYYY-MM-DD HH:mm:ss"
              value-format="YYYY-MM-DD H:m:s"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="save">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
  </span>
    </el-dialog>



  </div>
  <div class="demo-pagination-block">
    <el-pagination
        v-model:currentPage="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 20]"
        :small="small"

        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: 'Home',
  components: {},
  data() {
    return {
      form: {
        cishu: 1,
        name: '',
        time: ''
      },
      search: '',
      currentPage: 1,
      dialogVisible: false,
      tableData: [],
      pageSize: 10,
      total: 20
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      request.get("/user/find", {
        params:{
          PN: this.currentPage,
          PS: this.pageSize,
          search: this.search
        }
      }).then(res => {
        console.log(res)
        this.tableData=res.data.records
        this.total=res.data.total
      })

    },
    handleEdit(row){
      this.form=JSON.parse(JSON.stringify(row))
      this.dialogVisible=true
    },
    handleDelete(id){
      request.delete("/user/"+id).then(res=>{
        if(res.code ==='0'){
          this.$message({
            type:"success",
            message:"删除成功"
          })
        }else {
          this.$message({
            type:"error",
            message:res.msg
          })
        }
        this.load()
      })

    },
    add() {
      this.dialogVisible = true
      this.form = {}

    },
    save() {
      if(this.form.id){
        request.put("/user/update", this.form).then(res => {
          console.log(res)
          if(res.code ==='0'){
            this.$message({
              type:"success",
              message:"更新成功"
            })
          }else {
            this.$message({
              type:"error",
              message:res.msg
            })
          }
        })
        this.dialogVisible=false
        this.load()

      }else
      {
        request.post("/user/save", this.form).then(res => {
          console.log(res)
          if(res.code ==='0'){
            this.$message({
              type:"success",
              message:"添加成功"
            })
          }else {
            this.$message({
              type:"error",
              message:res.msg
            })
          }
        })
        this.dialogVisible=false
        this.load()
      }

    },
    handleSizeChange(pageSize){
        this.pageSize=pageSize
         this.load()
    },
    handleCurrentChange(pageNum){
      this.currentPage=pageNum
      this.load()

    }

  }
}
</script>
